package com.auto.learning.pay.alipay;

import java.sql.SQLException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.auto.learning.R;
import com.auto.learning.activity.MyMoneyActivity;
import com.auto.learning.activity.SubscriptionActivity;

import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.bean.UserVipInfo;
import com.auto.learning.event.BuySuccessEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.pay.UserBuyUtils;
import com.auto.learning.pay.wechatpay.WechatPayCommon;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.MyToast;
import com.auto.learning.wxapi.WXPayEntryActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@SuppressLint("HandlerLeak")
public class AliPay {

	private static final int SDK_PAY_FLAG = 1;
	private static final int SDK_CHECK_FLAG = 2;
	private Context mContext;
	private String payUrl;
	private int money;
	
		
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SDK_PAY_FLAG: {
				Result resultObj = new Result((String) msg.obj);
				String resultStatus = resultObj.resultStatus;

				// 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
				if (TextUtils.equals(resultStatus, "9000")) {
					Toast.makeText(mContext,mContext.getString(R.string.poa_paysuccess),Toast.LENGTH_SHORT).show();
					if (mContext instanceof MyMoneyActivity) {
						if(TemporaryVariable.bookmoney.getType() != 0){
							new UserBuyUtils(mContext,TemporaryVariable.bookmoney, 6).tomoney();
						}
						((MyMoneyActivity)mContext).rechargeSuccess(money);
					}
					if(mContext instanceof SubscriptionActivity){
						
						new UserBuyUtils(mContext,TemporaryVariable.bookmoney, 6).tomoney();
					}
				} else {
					// 判断resultStatus 为非“9000”则代表可能支付失败  
					// “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
					if (TextUtils.equals(resultStatus, "8000")) {
						Toast.makeText(mContext, mContext.getString(R.string.poa_payconfirm), Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(mContext, mContext.getString(R.string.poa_payfail), Toast.LENGTH_SHORT).show();
					}
				}
				break;
			}
			case SDK_CHECK_FLAG: {
				Toast.makeText(mContext, mContext.getString(R.string.poa_result) + msg.obj, Toast.LENGTH_SHORT).show();
				break;
			}
			}
		};
	};

	/**
	 * 构造订单信息
	 * @param context
	 * @param payInfo
	 */
	public AliPay(Context context, String payInfo,int money) {
		Log.d("payInfo", payInfo);
		this.mContext = context;
		this.payUrl = payInfo;
		this.money = money;
		payToAli();
	}

	/**
	 * 调用支付接口
	 */
	public void payToAli() {
		Runnable payRunnable = new Runnable() {
			@Override
			public void run() {
				PayTask alipay = new PayTask((Activity) mContext);  // 构造PayTask 对象
				String result = alipay.pay(payUrl);  // 调用支付接口
				Message msg = new Message();
				msg.what = SDK_PAY_FLAG;
				msg.obj = result;
				mHandler.sendMessage(msg);
			}
		};
		Thread payThread = new Thread(payRunnable);
		payThread.start();
	}
}
